package o;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

final class uo {
    private final a ˋ = new a();
    private final Map<String, d> ˏ = new HashMap();

    static class a {
        private final Queue<d> ˎ = new ArrayDeque();

        a() {
        }

        d ˊ() {
            synchronized (this.ˎ) {
                d dVar = (d) this.ˎ.poll();
            }
            if (dVar == null) {
                return new d();
            }
            return dVar;
        }

        void ˏ(d dVar) {
            synchronized (this.ˎ) {
                if (this.ˎ.size() < 10) {
                    this.ˎ.offer(dVar);
                }
            }
        }
    }

    static class d {
        final Lock ˊ = new ReentrantLock();
        int ॱ;

        d() {
        }
    }

    uo() {
    }

    void ˋ(String str) {
        d dVar;
        synchronized (this) {
            dVar = (d) this.ˏ.get(str);
            if (dVar == null) {
                dVar = this.ˋ.ˊ();
                this.ˏ.put(str, dVar);
            }
            dVar.ॱ++;
        }
        dVar.ˊ.lock();
    }

    void ॱ(String str) {
        d dVar;
        synchronized (this) {
            dVar = (d) aas.ˎ(this.ˏ.get(str));
            if (dVar.ॱ < 1) {
                throw new IllegalStateException("Cannot release a lock that is not held, safeKey: " + str + ", interestedThreads: " + dVar.ॱ);
            }
            dVar.ॱ--;
            if (dVar.ॱ == 0) {
                d dVar2 = (d) this.ˏ.remove(str);
                if (dVar2.equals(dVar)) {
                    this.ˋ.ˏ(dVar2);
                } else {
                    throw new IllegalStateException("Removed the wrong lock, expected to remove: " + dVar + ", but actually removed: " + dVar2 + ", safeKey: " + str);
                }
            }
        }
        dVar.ˊ.unlock();
    }
}
